// components/xm-picker/index.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    columns: {
      type: Array,
      value: ['1','2']
    },
    defaultValue: {},
    placeholder: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    showPicker: false,
    // columns: ['1', '2', '3'],
    checkedIndex: 0,
    checked: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onConfirm(event) {
      const { picker, value, index } = event.detail;
      this.setData({showPicker: false, checkedIndex: index})
      this.triggerEvent('confirm', {index: index, value: value})
    },
    onCancel() {
      this.setData({showPicker: false})
    },
    onPicker() {
      this.setData({showPicker: true})
    },
    getCheckedItem() {
      return {
        checked: this.data.checked,
        index: this.data.checkedIndex,
      }
    }
  },
  lifetimes: {
    ready() {
      if (this.data.defaultValue) {
        const itemIndex = this.data.columns.findIndex(item => this.data.defaultValue === item)
        if (itemIndex > -1) {
          this.setData({
            checked: this.data.columns[itemIndex],
            checkedIndex: itemIndex
          })
          return
        }
      }
      this.setData({
        checked: this.data.columns[0],
        checkedIndex: 0
      })
    },
  },
  observers: {
    'checkedIndex': function (value) {
      this.setData({
        checked: this.data.columns[value]
      })
    }
  }
})